<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增参数')" />
    <th:block th:include="include :: select2-css" />
    <th:block th:include="include :: bootstrap-select-css" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-config-add" name="form-config-add" >

        <h4 class="form-header h4">数据模型添加</h4>

        <div class="row">
            <div class="form-group">
                <div class="col-sm-3">
                    <div th:object="${user}">
                        <input th:value="*{userId}" class="form-control" readonly="readonly" type="hidden" name="createId"/>
                        <input th:value="*{userName}" class="form-control" readonly="readonly" type="hidden" name="createName"/>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-sm-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">数据模型名称：</label>
                    <div class="col-sm-7">
                        <input id="moduleName" name="moduleName" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-sm-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">发布状态：</label>
                    <div class="col-sm-7">
                        <select id="onlineStatus" class="form-control" required th:with="type=${@dict.getType('online_status')}" th:name="onlineStatus">
                            <option value="">--选择发布状态查询--</option>
                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                        </select>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-sm-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">模型分类：</label>
                    <div class="col-sm-7">
                        <select id="typeId" class="form-control" required th:with="type=${moduleTypes}" th:name="typeId">
                            <option value="">--选择模型分类查询--</option>
                            <option th:each="dict : ${type}" th:text="${dict.typeName}" th:value="${dict.id}"></option>
                        </select>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="form-group">
                <div class="col-sm-12">
                    <label class="col-sm-3  control-label is-required">数据模型封面上传：</label>
                    <div class="col-sm-7">
                        <input
                                id="file"
                                name="file"
                                data-show-preview="true"
                                type="file"
                                multiple
                                data-max-file-count="1"
                                data-theme="fas">
                    </div>
                </div>
            </div>
        </div>

        <h4 class="form-header h4">数据格式添加</h4>

        <div class="row">
            <div class="col-sm-12">
                <div class="form-group key">

                </div>
            </div>
        </div>

        <button type="button" onclick="addKeyInput()" class="btn btn-w-m btn-success">添加数据格式key值输入框</button>
        <button type="button" onclick="removeInput()" class="btn btn-w-m btn-danger">删除key值输入框</button>

        <div class="col-sm-offset-5 col-sm-10">
            <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存</button>&nbsp;
            <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭</button>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: select2-js" />
<th:block th:include="include :: bootstrap-select-js" />
<th:block th:include="include :: bootstrap-fileinput-js" />
<script type="text/javascript">
    var prefix = ctx + "system/DataModule";
    var divNum = 0;

    $(document).ready(function () {
        $("#file").fileinput({
            'theme': 'explorer-fas',
            'uploadUrl': '#',
            initialPreviewAsData: true,
            autoReplace:true,
            showRemove:false,
            showUpload:false,
            layoutTemplates :{
                actionDelete:'', //去除上传预览的缩略图中的删除图标
                actionUpload:'',//去除上传预览缩略图中的上传图片；
                actionZoom:''   //去除上传预览缩略图中的查看详情预览的缩略图标。
            }
        });
    });

    $("#form-config-add").validate({
        onkeyup: false,
        onSubmit: true,
        rules: {
            moduleName: {
                required: true,
                remote: {
                    url: prefix + "/checkDataModuleNameUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "id": '',
                        "moduleName": function () {
                            return $.common.trim($("#moduleName").val());
                        }
                    },
                    dataFilter: function(data, type) {
                        return data;
                    }
                }
            }
        },
        messages: {
            "moduleName": {
                remote: "模型名不能重复"
            }
        },
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            var formData = new FormData($('#form-config-add')[0]);

            var fileInput = $('#file').get(0).files[0];
            console.info(fileInput);
            if(!fileInput){
                $.modal.alertWarning("请上传封面");
                return;
            }

            if (divNum == 0){
                $.modal.alertWarning("请生成格式key值");
                return;
            }

            var sss = $('input[name=dataRule]');
            var asd = true;
            for(j = 0,len=sss.length-2; j <= len; j++) {
                for(i = j+1,len2=sss.length-1; i <= len2; i++) {
                    if(sss[j].value==sss[i].value){
                        $.modal.alertError("value值不能相同！！！");
                        asd = false;
                        return;
                    }
                }
            }

            if(asd == false){
               return;
            }

            formData.append('file', $('#file')[0].files[0]);

            $.ajax({
                url: prefix + "/add",
                type: 'post',
                cache: false,
                data: formData,
                processData: false,
                contentType: false,
                dataType: "json",
                success: function(result) {
                    $.operate.successCallback(result);
                }
            });
        }
    }


    function addKeyInput () {

        var row = $("<div class=\"row\">\n" +
            "            <div class=\"col-sm-12\">\n" +
            "                <div class=\"form-group key\">\n" +
            "                    <div class=\"col-sm-3\">\n" +
            "                        <input name=\"dataRule\" class=\"form-control\" type=\"text\" required>\n" +
            "                    </div>\n" +
            "                </div>\n" +
            "            </div>\n" +
            "        </div>");

        var input = $("<div class=\"col-sm-3\">\n" +
            "               <input name=\"dataRule\" class=\"form-control\" type=\"text\" required>\n" +
            "          </div>");


        if (divNum == 0){
            $(".key:last").append(input);
            console.log("num = 0 : " + divNum);
        }else if (divNum%4 != 0){
            $(".key:last").append(input);
            console.log("num < 4 : " + divNum);
        }else if(divNum%4 == 0) {
            $(".row:last").after(row);
            console.log("num = 4 : " + divNum);
        }

        divNum = divNum + 1;

    }

    function removeInput() {

        if(divNum != 0){
            divNum--;
            if(divNum == 0){
                $(".row:last .key div:last").remove()
            } else if(divNum % 4 != 0){
                $(".row:last .key div:last").remove()
            }else if(divNum % 4 == 0){
                $(".row:last").remove();
            }

            console.log("remove divNum = " + divNum)
        }
    }
</script>
</body>

</html>
